<template>
  <!-- 文档查看 -->
  <div class="pdf" v-loading="loading" element-loading-text="加载中">
    <!-- <iframe
      :frameborder="0"
      width="100%"
      height="100%"
      src=""
      ref="pdf"
      @click="load"
    /> -->
    <!-- 显示pdf文件 -->
    <pdf
      :src="src"
      v-for="i in pdfTotal"
      :key="i"
      :page="i"
      @error="error"
      @page-loaded="pageLoaded"
    ></pdf>
    <div class="no-data" v-if="isNoData">暂无数据</div>
  </div>
</template>

<script>
import pdf from "vue-pdf";
export default {
  components: { pdf },
  props: {
    fileUrl: {
      type: String,
    },
  },
  data() {
    return {
      // pdf: null,
      src: "",
      pdfTotal: 1, // pdf总页数
      loading: true, //加载中
      isNoData: false,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.setUrl();
    });
  },
  methods: {
    setUrl() {
      if (this.fileUrl) {
        // const fileUrl = `${window.VUE_APP_BASE_URL}/${this.fileUrl}#toolbar=0&navpanes=0&scrollbar=0`;
        const fileUrl = `${window.VUE_APP_BASE_URL}/${this.fileUrl}`;
        // console.log(123, fileUrl);
        this.src = pdf.createLoadingTask(fileUrl);
        console.log(456, this.src);
        this.src.promise.then((res) => {
          // console.log(789, res);
          this.pdfTotal = res.numPages;
        });
      }
    },
    pageLoaded(e) {
      // 加载完成后
      console.log(e, 222);
      if (e >= 1) {
        this.loading = false;
      }
      this.isNoData = false;
    },
    error(err) {
      this.loading = false;
      this.isNoData = true;
      this.pdfTotal = 1;
      console.log(999, err);
    },
  },
  watch: {
    deep: true,
    fileUrl() {
      this.setUrl();
    },
  },
};
</script>

<style lang="scss">
@import "~@/styles/mixin.scss";
.pdf {
  @include scrollBar;
  overflow-y: auto;
  margin: 0.2rem;
  width: auto;
  height: 80vh;
  border: 1px solid #459beb;
  border-radius: 0.1rem;
  background: white;
  position: relative;
  ::v-deep .el-loading-spinner {
    top: 35.5vh;
  }
  #viewer {
    #toolbar {
      #end {
        display: none;
      }
    }
  }
  .no-data {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 50%;
    text-align: center;
    color: #666666;
  }
}
</style>